package com.kotiln.lgc.guidepagedemo.viewflipper;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kotiln.lgc.guidepagedemo.MainActivity;
import com.kotiln.lgc.guidepagedemo.R;

/**
 * @author $
 * @fileName ViewFlipperActivity
 * @date2021/3/22 9:57
 * @describe TODO
 * viewflipper实现引导页效果
 * @desc
 */
public class ViewFlipperActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

    private String TAG = ViewFlipperActivity.class.getSimpleName();
    private ViewFlipper viewFlipper;
    private LinearLayout llIndictor;
    private Button btn;

    private GestureDetector getGesDetector;
    private int index = 0;//当前是第几屏

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewflipper);
        viewFlipper = findViewById(R.id.viewflipper);
        llIndictor = findViewById(R.id.llIndictor);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ViewFlipperActivity.this, MainActivity.class));
                finish();
            }
        });

        //初始化指示器
        initIndicator();
    }

    private void initIndicator() {
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, width);
        lp.rightMargin = 2 * width;
        for (int i = 0; i < viewFlipper.getChildCount(); i++) {
            View view = new View(this);
            view.setId(i);
            view.setBackgroundResource(i == 0 ? R.drawable.dot_focus : R.drawable.dot_normal);
            view.setLayoutParams(lp);
            llIndictor.addView(view, i);
        }
        getGesDetector = new GestureDetector(this);
    }


    //手势监听
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    //滑动过程中
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {

        //下一页
        if (e1.getX() > e2.getX()) {
            Log.i(TAG, "NEXT");
            viewFlipper.showNext();
            index = index < 3 ? index + 1 : 0;
        } else if (e2.getX() > e1.getX()) {
            //上一页
            Log.i(TAG, "PREVIOUS");
            viewFlipper.showPrevious();
            index = index > 0 ? index - 1 : 3;
        } else {
            return false;
        }

        //改变指示器的状态
        changeIndicator();

        return true;
    }

    private void changeIndicator() {
        for (int i = 0; i < viewFlipper.getChildCount(); i++) {
            llIndictor.getChildAt(i).setBackgroundResource(index == i ? R.drawable.dot_focus : R.drawable.dot_normal);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return getGesDetector.onTouchEvent(event);
    }
}
